﻿using System;
using System.IO;
using System.ServiceProcess;

namespace X18.Gather.GatherService
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string[] args)
        {
            if (Environment.UserInteractive)
            {
                try
                {
                    var gather = new GatherService();
                    gather.Init();
                    gather.Start(args);
                    Console.WriteLine("Press any key to contiune...");
                    Console.ReadLine();
                    gather.Stop();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + "\n\r" + ex.StackTrace);
                    Console.ReadLine();
                }
            }
            else
            {
                try
                {
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[]
                    {
                        new ServiceImpl()
                    };
                    ServiceBase.Run(ServicesToRun);
                }
                catch (Exception ex)
                {
                    Program.WriteLogLine(ex.Message + ex.StackTrace);
                    throw ex;
                }
            }
        }

        public static void WriteLogLine(string message)
        {
            try
            {
                string text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../Logs/");
                if (!Directory.Exists(text))
                {
                    Directory.CreateDirectory(text);
                }
                text = Path.Combine(text, "GatherService.log");
                Program.InternalWriteLogLine(text, message);
            }
            catch (Exception ex)
            {
                using (StreamWriter streamWriter = new StreamWriter(File.Open(Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "X18_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log"), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)))
                {
                    streamWriter.WriteLine("X18 写日志出现异常：" + ex.Message + ex.StackTrace);
                }
            }
            Console.WriteLine(message);
        }

        private static void InternalWriteLogLine(string LogFile, string message)
        {
            FileStream fileStream;
            if (File.Exists(LogFile))
            {
                fileStream = File.Open(LogFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            }
            else
            {
                fileStream = File.Open(LogFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            }
            using (StreamWriter streamWriter = new StreamWriter(fileStream))
            {
                streamWriter.WriteLine(string.Format("{0} {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message));
                streamWriter.Flush();
            }
        }
    }
}
